<?php
/**
 * 每分钟检查品牌过期或续费 更新品牌下商品的住友说以及品牌说计数器
 */

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('display_errors', 1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\brand\BrandTypeChange as cBrandTypeChange;
use Biz\model\wiki\ObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;

/**
 * 更新品牌下商品的住友说以及品牌说计数器
 * @param $brandId
 * @return bool
 */
function counterModify($brandId)
{
    $wikiList = mWiki::getInfosByBrandIds($brandId);
    $wikiIdArr = array_keys($wikiList);
    $objRelateWikiList = ObjRelateWiki::select('wiki_id', ['wiki_id' => $wikiIdArr]);
    if ($objRelateWikiList) {
        $objRelateWikiList = array_unique($objRelateWikiList);
        foreach ($objRelateWikiList as $wikiId) {
            if (isset($wikiList[$wikiId])) {
                $basicId = $wikiList[$wikiId]['basic_id'] ?? 0;
                if ($basicId > 0) {
                    println("#doing#brand_id=" . $brandId . "wiki_id=" . $wikiId . "#basic_id=" . $basicId);
                    $res = sObjRelateWiki::syncWikiCommentCounter($wikiId, $basicId);
                    if (!$res) {
                        println("#done#brand_id=" . $brandId . "#过期或续费品牌-更新住友说计数器失败#wiki_id=" . $wikiId . "#basic_id=" . $basicId);
                    }
                }
            }
        }
    }

    return true;
}

/**
 * 执行更新操作
 * @return void
 */
function actionDo()
{
    while (cBrandTypeChange::getCount()) {
        $brandId = cBrandTypeChange::getBrand();
        if ($brandId) {
            println("#start#brand_id=" . $brandId);
            counterModify($brandId);
            println("#end#brand_id=" . $brandId);
        }
    }
}

function println($msg, $prefix = 'wiki_recount_after_brand_type_change')
{
    echo $prefix . $msg . PHP_EOL;
}

actionDo();